package com.example.demo.springbootdemo.demos;

import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/login")
    public boolean login(String username, String password, HttpSession session) {
        // 校验
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return false;
        }
        if ("admin".equals(username) && "admin".equals(password)) {
            // 设置内容
            System.out.println("success");
            session.setAttribute("username", "admin");
            return true;
        }
        return false;
    }

    // 获取用户信息
    @RequestMapping("/getUserInfo")
    public String getUserInfo(HttpSession session) {
        return (String) session.getAttribute("username");
    }
}
